
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       bitmask.h
  * @author     baiyang
  * @date       2022-11-5
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <rtconfig.h>

#include <stdint.h>
#include <stdbool.h>
/*-----------------------------------macro------------------------------------*/
#define BITMASK_NUMBITS_TO_NUMWORDS(num_bits) ((num_bits+31)/32)
/*----------------------------------typedef-----------------------------------*/
typedef struct mb_bitmask* mb_bitmask_t;
typedef const struct mb_bitmask* mb_bitmask_const_t;


/** @ 
  * @brief  bits[(num_bits+31)/32];
  */
struct mb_bitmask {
    uint16_t numbits;
    uint16_t numwords;
    uint32_t *bits;
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void mb_bitmask_init(mb_bitmask_t bitmask, uint16_t num_bits, uint32_t *pool);

#ifdef RT_USING_HEAP
mb_bitmask_t mb_bitmask_create(uint16_t num_bits);
void mb_bitmask_destroy(mb_bitmask_t bitmask);
#endif

void mb_bitmask_copy(mb_bitmask_t dst, mb_bitmask_const_t src);

// clear all bits
void mb_bitmask_clearall(mb_bitmask_t bitmask);

// set given bitnumber
void mb_bitmask_set(mb_bitmask_t bitmask, uint16_t bit);

// set all bits
void mb_bitmask_setall(mb_bitmask_t bitmask);

// clear given bitnumber
void mb_bitmask_clear(mb_bitmask_t bitmask, uint16_t bit);

// set given bitnumber to on/off
void mb_bitmask_setonoff(mb_bitmask_t bitmask, uint16_t bit, bool onoff);

// return true if given bitnumber is set
bool mb_bitmask_get(mb_bitmask_const_t bitmask, uint16_t bit);

// return true if all bits are clear
bool mb_bitmask_empty(mb_bitmask_const_t bitmask);

// return number of bits set
uint16_t mb_bitmask_count(mb_bitmask_const_t bitmask);

// return first bit set, or -1 if none set
int16_t mb_bitmask_first_set(mb_bitmask_const_t bitmask);

// return number of bits available
uint16_t mb_bitmask_size(mb_bitmask_const_t bitmask);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



